package com.example.testnews.fragmant;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.testnews.HttpUtil.HttpUtil;
import com.example.testnews.HttpUtil.Util;
import com.example.testnews.R;
import com.example.testnews.adapter.HomeAdapter;
import com.example.testnews.bean.NewsList;

import java.io.IOException;

public class NewsFragment extends Fragment {
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private View view;
    private final String TAG="0000";
    HomeAdapter homeAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view =inflater.inflate(R.layout.fragment_news, container, false);
        homeAdapter = new HomeAdapter(getActivity());
        initView();
//        HomeAdapter.setOnItemClickListener(new HomeAdapter.OnItemClickListener() {
//            @Override
//            public void onClick(int position) {
//                //Toast.makeText(getContext(),"click" ,Toast.LENGTH_SHORT).show();
//                //String data = "News Detail Is Showing";
//                List<News> data =
//                Intent intent = new Intent(getContext(), NewsActivity.class);
//                intent.putExtra("extra_data",data);
//                startActivity(intent);
//
//            }
//        });
        return view;
    }

    private void initView(){
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(homeAdapter);
        swipeRefreshLayout=(SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh);
        swipeRefreshLayout.setColorSchemeResources(R.color.design_default_color_primary);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        getNews();

    }

    private void refresh() {
        page++;
        getNews();
    }

    private int page = 1;
    private void getNews(){

        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    String result = HttpUtil.sendOkHttpRequest("https://api.tianapi.com/topnews/index?key=d4f64db4ff1bbae9134d5a71b9d98c20&page="+ page);
                    NewsList newsList = Util.parseJsonWithGson(result);
                    getActivity().runOnUiThread(()->{
                        homeAdapter.setNews(newsList.newsList);
                        swipeRefreshLayout.setRefreshing(false);
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}
